package com.threaddemo3;

import java.util.concurrent.*;

public class CallableDemo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //用法
        //1、创建Callable:可以有返回值 可以抛出异常
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(1000);
                return "Hello Callable!";
            }
        };
        //2、放到线程池中 使用线程池配合使用
        ExecutorService es = Executors.newSingleThreadExecutor();

        //3、将Callable 实例提交到线程池中
        Future<String> future = es.submit(callable);

        //4、可以获得返回值
        String ret = future.get();

        System.out.println("ret = " + ret);

        //5、关闭 ExecutorService
        es.shutdown();

    }
}
